<?php
class dirsearch {
	
	//timer
	public $timer_start			= null;
	public $timer_end			= null;
	public $timer_total			= null;
	
	//detectors
	public $os					= null;
		
	public function __construct($dir="/") {
		$this->checkOs();
		
		echo '<pre>';
		//execsearch test
		$this->timer_start = microtime();
		print_r($this->execSearch($dir));
		$this->timer_end = microtime();
		$this->timer_total = $this->timer_end-$this->timer_start;
		echo "exec output time: $this->timer_total\n";
		
		//opendir test
		$this->timer_start = microtime();
		print_r($this->opendirSearch($dir));
		$this->timer_end = microtime();
		$this->timer_total = $this->timer_end-$this->timer_start;
		echo "opendir output time: $this->timer_total\n";
		
		//scandir test
		$this->timer_start = microtime();
		print_r($this->scandirSearch($dir));
		$this->timer_end = microtime();
		$this->timer_total = $this->timer_end-$this->timer_start;
		echo "scandir output time: $this->timer_total\n";
	}
	
	private function checkOs() {
		if(DIRECTORY_SEPARATOR == "/") {
			$this->os = "linux";
		}
		if(DIRECTORY_SEPARATOR == "\\") {
			$this->os = "windows";
		}
	}
	
	public function execSearch($dir="/") {
		if($this->os=="windows") {
			return $this->execute("dir /A $dir");
		}
		elseif($this->os=="linux") {
			return $this->execute("ls -a $dir");
		}
		else {
			return null;
		}
	}
	
	public function opendirSearch($dir="/") {
		$res = null;
                $handle = null;
		if($handle = opendir($dir)) {
		    while(false !== ($entry = readdir($handle))) {
		        $res .= "$entry\n";
		    }
		    closedir($handle);
		}
		return $res;
	}
	
	public function scandirSearch($dir="/") {
		return scandir($dir);
	}
	
	private function execute($cmd){
                $result = null;
		if(function_exists("exec")) {
			echo "using exec\n";
			@exec($cmd,$result);
			$result = join("\n",$result);
		}
		elseif(function_exists("shell_exec")) {
			echo "using shell_exec\n";
			$result = @shell_exec($cmd);
		}
		elseif(function_exists("system")) {
			echo "using system\n";
			@ob_start();
			@system($cmd);
			$result = @ob_get_contents();
			@ob_end_clean();
		}
		elseif(function_exists("passthru")) {
			echo "using passthru\n";
			@ob_start();
			@passthru($cmd);
			$result = @ob_get_contents();
			@ob_end_clean();
		}
		elseif(@is_resource($f = @popen($cmd,"r"))) {
			echo "using popen\n";
			while(!@feof($f)) {
				$result .= @fread($f,8192);
			}
			@pclose($f);
		}
		else {
			echo "using special quotes\n";
			$result = `$cmd`;
		}
		return $result;
	}
	
}

$dirsearch = new dirsearch("E:".DIRECTORY_SEPARATOR);
print_r($dirsearch);
?>